﻿using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using Nemerle.Contrib;

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Spark;
using Spark.Compiler;
using Spark.Parser.Code;
using Spark.Compiler.ChunkVisitors;
using Spark.Compiler.NodeVisitors;
using Spark.Parser.Markup;

namespace NRails.Spark
{
  class MatchExtension : ISparkExtension
  {
      node : ElementNode;
      
      public this(node : ElementNode)
      {
          this.node = node;
      }
      
      public VisitNode(visitor : INodeVisitor, body : IList[Node], _chunks : IList[Chunk]) : void
      {
          visitor.Accept(body.RemoveEmptyLiterals());
      }

      public VisitChunk(visitor : IChunkVisitor, location : OutputLocation, chunks : IList[Chunk], output : StringBuilder) : void 
      {
          when (location == OutputLocation.RenderMethod)
          {
              def expr = node.Attributes.Attr("expr") ?? node.Attributes.Attr("match");
              
              when (expr == null)
                throw CompilerException("requre match expression in expr attribute");

              _ = output.AppendLine($"match ($(expr.AsCode())) {");
              visitor.Accept(chunks);
              _ = output.AppendLine("}");
          }
      }
  }
}
